package com.mapler.utility;

import com.mapler.model.SModel;
import com.mapler.service.INotifier;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

/**
 *
 * @author mithun
 */
public class W4MUtil {

    public static void clickReNewLink(WebDriver driver, SModel sModel, INotifier iNotifier) {
        try {
            if (sModel.getRenewItems() != null && !sModel.getRenewItems().isEmpty()) {
                int renew = Integer.parseInt(sModel.getRenewItems());
                for (int i = 1; i <= renew; i++) {
                    List<WebElement> aelements = driver.findElements(By.tagName("a"));
                    for (WebElement elementLink : aelements) {
                        if ((elementLink.getAttribute("href") != null && elementLink.getAttribute("href").contains("https://accounts.craigslist.org/login"))) {
                            elementLink.click();
                            break;
                        }
                    }

                    Util.wait(3);

                    List<WebElement> elements = driver.findElements(By.tagName("input"));
                    for (WebElement elementLink : elements) {
                        if ((elementLink.getAttribute("value") != null && elementLink.getAttribute("value").contains("renew"))
                                && (elementLink.getAttribute("type") != null && elementLink.getAttribute("type").contains("submit"))) {
                            elementLink.click();
                            break;
                        }
                    }
                }
            }
        } catch (Throwable ex) {
            iNotifier.notify("Unable to click on renewlink...");
            ex.printStackTrace();
        }
    }

    public static void main(String s[]) throws Exception {
        SModel sModel = new SModel();
        sModel.setDriver("FF");
        WebDriver driver = null;
        driver.get("https://accounts.craigslist.org/");
        WebElement email = driver.findElement(By.id("inputEmailHandle"));
        email.sendKeys("hollyrussell34@gmail.com");

        WebElement pass = driver.findElement(By.id("inputPassword"));
        pass.sendKeys("bomnajdo");
        Util.byButton(driver, "button", "type", "submit");
        List<WebElement> elements = driver.findElements(By.tagName("input"));
        for (WebElement elementLink : elements) {
            if ((elementLink.getAttribute("value") != null && elementLink.getAttribute("value").contains("renew"))
                    && (elementLink.getAttribute("type") != null && elementLink.getAttribute("type").contains("submit"))) {
                elementLink.click();
                break;
            }
        }

    }
}
